package lc.q1051_1100;

import java.util.Arrays;

public class Q1051 {

    public int heightChecker(int[] heights) {
        int max = Arrays.stream(heights).max().getAsInt();
        int[] counts = new int[max + 1];
        for (int height : heights) {
            ++ counts[height];
        }
        int idx = 0, ans = 0;
        for (int i = 1; i <= max; i++) {
            for (int i1 = 1; i1 <= counts[i]; i1++) {
                if (heights[idx] != i) {
                    ans ++;
                }
                idx ++;
            }
        }
        return ans;
    }

}
